<!DOCTYPE html>

<html lang="en" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Defining sounds &#8212; Pytch  documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=03e43079" />
    <link rel="stylesheet" type="text/css" href="../../_static/classic.css?v=36340f97" />
    <link rel="stylesheet" type="text/css" href="../../_static/css/pytch-classic.css?v=0321735e" />
    
    <script src="../../_static/documentation_options.js?v=7f41d439"></script>
    <script src="../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../../_static/favicon.ico"/>
    <link rel="author" title="About these documents" href="../../about.html" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Running many scripts at once" href="multi-threading.html" />
    <link rel="prev" title="Defining backdrops" href="backdrop-specs.html" /> 
  </head><body>
<div class="NavBar">
  <a href="../../../app/"><h1>Pytch</h1></a>
  <ul>
    <a href="https://pytch.scss.tcd.ie/"><li>About Pytch</li></a>
    <a href="../../index.html"><li>Help</li></a>
    <a href="../../../app/tutorials/"><li>Tutorials</li></a>
    <a href="../../../app/my-projects/"><li>My projects</li></a>
  </ul>
</div>
<div class="warning-work-in-progress">
  <p>These help pages are incomplete — we are working on it!</p>
</div>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="defining-sounds">
<span id="sound-specifications"></span><h1>Defining sounds<a class="headerlink" href="#defining-sounds" title="Link to this heading">¶</a></h1>
<p>Both Sprites and the Stage can have sounds.  You will usually define
these just by their filename, for example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">pytch</span>

<span class="k">class</span><span class="w"> </span><span class="nc">Kitten</span><span class="p">(</span><span class="n">pytch</span><span class="o">.</span><span class="n">Sprite</span><span class="p">):</span>
    <span class="n">Sounds</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;miaow.mp3&quot;</span><span class="p">,</span> <span class="s2">&quot;mew.mp3&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>And then you can say, for example, <code class="docutils literal notranslate"><span class="pre">self.start_sound(&quot;mew&quot;)</span></code>.  If
you want to have a different label for a sound, you can use a
<em>two-element tuple</em> like</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">pytch</span>

<span class="k">class</span><span class="w"> </span><span class="nc">Puppy</span><span class="p">(</span><span class="n">pytch</span><span class="o">.</span><span class="n">Sprite</span><span class="p">):</span>
    <span class="n">Sounds</span> <span class="o">=</span> <span class="p">[(</span><span class="s2">&quot;bark&quot;</span><span class="p">,</span> <span class="s2">&quot;big-loud-bark.mp3&quot;</span><span class="p">),</span> <span class="s2">&quot;growl.mp3&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>Here we will be able to say <code class="docutils literal notranslate"><span class="pre">self.start_sound(&quot;bark&quot;)</span></code>, which will
start playing the <code class="docutils literal notranslate"><span class="pre">big-loud-bark.mp3</span></code> file; or we can say
<code class="docutils literal notranslate"><span class="pre">self.start_sound(&quot;growl&quot;)</span></code>, which will start playing the
<code class="docutils literal notranslate"><span class="pre">growl.mp3</span></code> file.  This example shows that you can give some sounds
a custom label, and leave others with their default label.</p>
<section id="playing-sounds">
<h2>Playing sounds<a class="headerlink" href="#playing-sounds" title="Link to this heading">¶</a></h2>
<p>Once you have given your Sprite (or Stage) some sounds as above, see
the help about the <a class="reference internal" href="sprite.html#methods-playing-sounds"><span class="std std-ref">Making sounds</span></a>
methods to learn how to play them.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper"><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../webapp/user/index.html">Using the Pytch web app</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Writing Pytch programs</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="sprite.html">Sprites</a></li>
<li class="toctree-l2"><a class="reference internal" href="stage.html">The stage</a></li>
<li class="toctree-l2"><a class="reference internal" href="non-hat-blocks.html">Scratch blocks → Pytch functions and methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="pytch-module-functions.html">Functions in the pytch module</a></li>
<li class="toctree-l2"><a class="reference internal" href="hat-blocks.html">Scratch hat blocks → Pytch decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="costume-specs.html">Defining costumes</a></li>
<li class="toctree-l2"><a class="reference internal" href="backdrop-specs.html">Defining backdrops</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Defining sounds</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#playing-sounds">Playing sounds</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="multi-threading.html">Running many scripts at once</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../about.html">About Pytch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../contact.html">Contact</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../developer.html">Developer documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../legal/index.html">Legal information</a></li>
</ul>
<div class="docs-home-link"><hr>
  <ul>
    <li>
      <a href="../../index.html">Pytch help home</a>
    <li>
  </ul>
</div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  </body>
</html>